import axios from "axios";
import {nanoid} from "nanoid";

export default {
    namespaced:true,
    actions: {
        addPersonWang(context,value) {
            if (value.name.indexOf('王') === 0) {
                context.commit('ADD_PERSON',value);
            } else {
                alert('请添加一个姓王的名字');
            }
        },
        //通过发请求获得数据添加
        addPersonServer(context) {
            axios.get('https://api.xygeng.cn/one').then(response => {
                context.commit('ADD_PERSON',{id:nanoid(),name:response.data.data.content})
            },(err) => {
                alert('出错了，请重试',err);
            })
        }
    },
    mutations: {
        ADD_PERSON(state, value) {
            state.persons.unshift(value);
        }
    },
    state: {
        persons: [{id: '001', name: '张三'}]
    },
    getters: {
        FirstPersonName(state) {
            return state.persons[0].name;
        }
    }
}